package com.ym.b2b.order.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.ym.b2b.buyer.sqlbean.EmCBuyerAddress;
import com.ym.b2b.commons.dao.BaseDao;
import com.ym.b2b.order.dao.EmSOrderDao;
import com.ym.b2b.order.sqlbean.EmSOrder;
import com.zc.commons.base.Page;

@Repository
public class EmSOrderDaoImpl<T> extends BaseDao<T> implements EmSOrderDao {

    @Override
    public Page<EmSOrder> searchPageOrders(EmSOrder emSOrder, Page<EmSOrder> page) throws DataAccessException {
        page.setTotalRow(searchOrdersNum(emSOrder));
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("map", emSOrder);
        map.put("startrow", page.getStartRow());
        map.put("pagesize", page.getPageSize());
        map.put("pageable", page.getPageable());
        page.setRows(list("EmSOrder.searchPageOrders", map));
        return page;
    }

    @Override
    public Long searchOrdersNum(EmSOrder emSOrder) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emSOrder);
        return (Long) find("EmSOrder.searchOrdersNum", map);
    }

    @Override
    public Long getEmSOrderId() throws DataAccessException {
        return (Long) find("EmSOrder.getEmSOrderId", "");
    }

    @Override
    public void saveOrderUpdateEmSOrder(EmSOrder emSOrder) throws DataAccessException {

        if (emSOrder.getOrderId() != null && !"".equals(emSOrder.getOrderId())) {
            // update
            update("EmSOrder.updateEmSOrder", emSOrder);
        } else {
            // insert
            insert("EmSOrder.insertEmSOrder", emSOrder);
        }

    }

    @Override
    public void insertEmSOrder(EmSOrder emSOrder) throws DataAccessException {
        insert("EmSOrder.insertEmSOrder", emSOrder);

    }

    @Override
    public void updateEmSOrder(EmSOrder emSOrder) throws DataAccessException {
        update("EmSOrder.updateEmSOrder", emSOrder);

    }

    @Override
    public EmSOrder searchOrderDetail(EmSOrder emSOrder) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emSOrder);
        return (EmSOrder) find("EmSOrder.searchOrderDetail", map);
    }

    @Override
    public void deleteEmSOrder(EmSOrder emSOrder) throws DataAccessException {
        delete("EmSOrder.deleteEmSOrder", emSOrder);

    }

    @Override
    public void updateEmSOrderForConfirm(EmSOrder emSOrder) throws DataAccessException {
        update("EmSOrder.updateEmSOrderForConfirm", emSOrder);
    }

    @Override
    public void updateEmSOrderDelivery(EmSOrder emSOrder) throws DataAccessException {
        update("EmSOrder.updateEmSOrderDelivery", emSOrder);
    }

	@Override
	public EmSOrder getOrderInfo(EmSOrder emSOrder) throws DataAccessException {
		return (EmSOrder) find("EmSOrder.getOrderInfo", emSOrder);
	}

}
